package main

import (
	"log"
	"math/rand"
	"time"
)

type Seat int
type Bar chan Seat

func (bar Bar) ServeCustomer(c int) {
	log.Print("Customer#", c, "Entering into Bar")
	seat := <-bar // They need a seat
	log.Print("++ Customer#", c, " drinks at seat#", seat)
	time.Sleep(time.Second * time.Duration(2+rand.Intn(6)))
	log.Print("-- Customer#", c, "Leaves #", seat)
	bar <- seat // An available seat
}

func main() {
	rand.Seed(time.Now().UnixNano())

	// There are 10 seats
	bar24x7 := make(Bar, 10)

	for seatId := 0; seatId < cap(bar24x7); seatId++ {
		bar24x7 <- Seat(seatId)
	}

	for customerId := 0; ; customerId++ {
		time.Sleep(time.Second)
		go bar24x7.ServeCustomer(customerId)
	}

	//for {
	// Sleep doesn't block the program
	//time.Sleep(time.Second)
	//}
	select {}
}
